#include "finddialog.h"
/*   1.创建并初始化子窗口部件
 *   2.把子窗口放到布局中
 *   3.设置Tab键顺序
 *   4.建立信号-槽之间的连接
 *   5.实现对话框中的自定义槽
*/
#include<QtWidgets> //Qt最重要的头文件
FindDialog::FindDialog(QWidget*parent):QDialog(parent)   //把parent参数传递给基类构造函数
{
/*********1.创建并初始化子窗口部件**********************/
    label=new QLabel(tr("找什么(&W)"));  //字符串调用tr函数，翻译成其他语言标记
    lineEdit=new QLineEdit;
    label->setBuddy(lineEdit);  //使用快捷键w将光标放在文本框里

    caseCheckBox =new QCheckBox(tr("区分大小写(&C)"));
    backwardCheckBox=new QCheckBox(tr("向后查找(&B)"));

    findButton =new QPushButton(tr("开始查找(&F)"));
    closeButton=new QPushButton(tr("关闭"));
    findButton->setDefault(true);
    findButton->setEnabled(false);

/*********4.建立信号-槽之间地连接**********************/
    connect(lineEdit,SIGNAL(textChanged(QString)),             //信号和槽的参数是同一个
            this,SLOT(enabledFindButton(QString)));

    connect(findButton,SIGNAL(clicked()),
            this,SLOT(findClicked()));

    connect(closeButton,SIGNAL(clicked()),
            this,SLOT(close()));

/*********2.把子窗口放到布局中**********************/

    //一个布局就是一个盒子
    QHBoxLayout *topLeftLayout=new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);

    QVBoxLayout *leftLayout=new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);
    leftLayout->addWidget(backwardCheckBox);

    QVBoxLayout *rightLayout=new QVBoxLayout;
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    rightLayout->addStretch();   //填充空白

    QHBoxLayout *mainLayout=new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);

    this->setLayout(mainLayout);
    this->setWindowTitle(tr("查找对话框"));
    this->setFixedHeight(sizeHint().height());  //创建最佳高度

}
void FindDialog::enabledFindButton(const QString &text)
{
    findButton->setEnabled(!text.isEmpty());
}
void FindDialog::findClicked()
{
    QString text=lineEdit->text();
    Qt::CaseSensitivity cs=caseCheckBox->isChecked()?
                Qt::CaseSensitive:Qt::CaseInsensitive;

    if(backwardCheckBox->isChecked())
    {
        emit findPrevious(text,cs);
    }else{
        emit findNext(text,cs);
    }
}
